class Solution {
public:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    bool arr[51][51];
    int maxAreaOfIsland(vector<vector<int>>& g) {
        int m=g.size();
        int n=g[0].size();
        int count=0;
        for(int i=0;i<m;++i)
        {
            for(int j=0;j<n;++j)
            {
                if(g[i][j]==1&&arr[i][j]==false)
                {
                    count=max(count,bfs(g,i,j,m,n,count));
                }
            }
        }
        return count;
    }

    int bfs(vector<vector<int>>& g,int i,int j,int m,int n,int count)
    {
        queue<pair<int,int>> q;
        q.push({i,j});
        arr[i][j]=true;
        count=1;
        while(q.size())
        {
            auto [x,y]=q.front();
            q.pop();
            for(int z=0;z<4;++z)
            {
                int a=x+dx[z];
                int b=y+dy[z];
                if(a>=0&&a<m&&b>=0&&b<n&&g[a][b]==1&&arr[a][b]==false)
                {
                    count++;
                    q.push({a,b});
                    arr[a][b]=true;
                }
            }
        }
        return count;
    }
};
